var utils = require('../../utils/utils.js');
var app = getApp();
const domainUrl = app.globalData.domainUrl

Page({

  data: {
    hidden: true
  },

  onLoad: function (options) {
    var openid = wx.getStorageSync('my_openid');
    var defaultUrl = domainUrl + 'shuihuCardMenuListNew?openId='+openid;
    //utils.http(defaultUrl, this.processDoubanData);
    this.updateData(defaultUrl);
  },
  onShow: function() {
    this.onLoad();
  },

  updateData: function (url) {
    this.setData({
      hidden: false
    });
    utils.http(url, this.processDoubanData);
  },

  processDoubanData: function (bookList) {
    var books = [];
    console.log(bookList);
    for (var idx in bookList.data) {
      var book = bookList.data[idx];
      var title = book.name;
      var temp = {
        title: title,
        coverImg: book.url,
        mtd: book.mtd,
        type: book.type
      }
      books.push(temp);
    }
    this.setData({
      books: books,
      hidden: true
    });
    console.log(books);
  },
  //之前用来从豆瓣获取数据的方法
  getBooksList: function (url) {
    this.setData({
      hidden: false
    });
    var that = this;
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/xml'
      },
      success: function (res) {
        //console.log(res.data);
        that.processDoubanData(res.data);
      }
    })
  },

  naviToTujian: function (event) {
    console.log(event);
    var mtd = event.currentTarget.dataset.item.mtd;
    var type1 = event.currentTarget.dataset.item.type;

    if(mtd === 'none'){
      wx.showToast({
        title: '敬请期待',
        icon: 'success',
        duration: 1000,
        mask: false
      })
      return;
    }

    wx.navigateTo({
      url: '../books/books?mtd=' + mtd+'&type='+type1
    })
  },

  onShareAppMessage: function () {

  }
})